package com.example.hello.client.ButtonGroup;

import com.example.hello.BootstrapButtonGroup;
import com.github.gwtbootstrap.client.ui.constants.ToggleType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentContainerConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapButtonGroup.class)
public class BootstrapButtonGroupConnector extends AbstractComponentContainerConnector {

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapButtonGroup.class);
    }

    @Override
    public BootstrapButtonGroupState getState() {
        return (BootstrapButtonGroupState) super.getState();
    }

    @Override
    public VBootstrapButtonGroup getWidget() {
        return (VBootstrapButtonGroup) super.getWidget();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapButtonGroupState state = getState();

        VBootstrapButtonGroup buttonGroup = getWidget();

        for (Connector connector : state.getButtons()) {
            buttonGroup.add(((ComponentConnector) connector).getWidget());
        }
        buttonGroup.setToggle(ToggleType.valueOf(state.getToggle()));
        buttonGroup.setVertical(state.isVertical());
    }

    @Override
    public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {

    }

    @Override
    public void updateCaption(ComponentConnector connector) {

    }

}